<?php
    if(isset($_POST["id"])) {$identifiant = $_POST["id"];} else{$identifiant = "";}
    if(isset($_POST["ligne"])) {$ligne = $_POST["ligne"];} else{$ligne = "";}
    if(isset($_POST["recherche"])) {$recherche = $_POST["recherche"];} else{$recherche = "";}
    if(isset($_POST["table"])) {$table = $_POST["table"];} else{$table = "";}
    
    require_once '../include/class.pdoSio.inc.php';
    
    if($identifiant <> "") {
        $pdo = PdoSio::getPdoSio();
        $req = $pdo->requeteSelection("SELECT id_utilisateur FROM utilisateurs WHERE identifiant = '".$identifiant."'");
        $id_etudiant = $req[0]['id_utilisateur'];
    } else {$id_etudiant = 0;}
    
    if($recherche == "") {
        echo "<div id='val'><input id='recherche' type='text' class='input-medium search-query' value='' />";
        if($identifiant <> "" && $id_etudiant == $ligne) {
            echo "<button class='btn'>Rechercher</button><br><br>
            <a href='index.php?uc=cv&action=ajouter&table=formation&ligne=".$ligne."' class='btn btn-xs btn-primary' name='ajouter'><span class='glyphicon glyphicon-plus'></span><img src='./styles/img/plus.png'/></a><br><br>
            <table class='table table-striped table-bordered'>
                <thead>
                    <th>Diplôme</th>
                    <th>Année de début</th>
                    <th>Année de fin</th>
                    <th>Etablissement</th>
                    <th>Actions<th>
                </thead>
                <tbody>";

            $pdo = PdoSio::getPdoSio();
            $etudiants = $pdo->requeteSelection("SELECT * FROM cv_formation WHERE id_utilisateur =".$ligne);
            foreach ($etudiants as $unEtudiant) {
                echo "<tr>
                        <td>".$unEtudiant['Diplome']."</td>
                        <td>".$unEtudiant['AnneeDeb']."</td>
                        <td>".$unEtudiant['AnneeFin']."</td>
                        <td>".$unEtudiant['Etab']."</td>
                        <td>
                            <a href='index.php?uc=cv&action=modifier&table=formation&ligne=".$unEtudiant['id_formation']."' class='btn btn-xs btn-primary' name='modifier'><span class='glyphicon glyphicon-pencil'></span><img src='./styles/img/stylo.png'></i></a>&nbsp;
                            <a href='index.php?uc=cv&action=supprimer&table=formation&ligne=".$unEtudiant['id_formation']."' class='btn btn-xs btn-primary' name='supprimer'><span class='glyphicon glyphicon-trash'></span><img src='./styles/img/moins.png'></i></a>
                        </td>";
                echo "</tr>";
            }
        } else {
            echo "<button class='btn'>Rechercher</button><br><br>
            <table class='table table-striped table-bordered'>
                <thead>
                    <th>Diplôme</th>
                    <th>Année de début</th>
                    <th>Année de fin</th>
                    <th>Etablissement</th>
                </thead>
                <tbody>";

            $pdo = PdoSio::getPdoSio();
            $etudiants = $pdo->requeteSelection("SELECT * FROM cv_formation WHERE id_utilisateur =".$ligne);
            foreach ($etudiants as $unEtudiant) {
                echo "<tr>
                        <td>".$unEtudiant['Diplome']."</td>
                        <td>".$unEtudiant['AnneeDeb']."</td>
                        <td>".$unEtudiant['AnneeFin']."</td>
                        <td>".$unEtudiant['Etab']."</td>
                    </tr>";
            }
        }
    } else {
        echo "<div id='val'><input id='recherche' type='text' class='input-medium search-query' value='".$recherche."' />";
        if($identifiant <> "" && $id_etudiant == $ligne) {
            echo "<button class='btn'>Rechercher</button><br><br>
            <a href='index.php?uc=cv&action=ajouter&table=formation&ligne=".$ligne."' class='btn btn-xs btn-primary' name='ajouter'><span class='glyphicon glyphicon-plus'></span><img src='./styles/img/plus.png'/></a><br><br>
            <table class='table table-striped table-bordered'>
                <thead>
                    <th>Diplôme</th>
                    <th>Année de début</th>
                    <th>Année de fin</th>
                    <th>Etablissement</th>
                    <th>Actions<th>
                </thead>
                <tbody>";

            $pdo = PdoSio::getPdoSio();
            if(is_numeric($recherche)) {
                $etudiants = $pdo->requeteSelection("SELECT * FROM cv_formation WHERE id_utilisateur =".$ligne." AND (AnneeDeb = ".$recherche." OR AnneeFin = ".$recherche.")");
            } else {
                $etudiants = $pdo->requeteSelection("SELECT * FROM cv_formation WHERE id_utilisateur =".$ligne." AND (Diplome LIKE '%".$recherche."%' OR Etab LIKE '%".$recherche."%')");
            }
            foreach ($etudiants as $unEtudiant) {
                echo "<tr>
                        <td>".$unEtudiant['Diplome']."</td>
                        <td>".$unEtudiant['AnneeDeb']."</td>
                        <td>".$unEtudiant['AnneeFin']."</td>
                        <td>".$unEtudiant['Etab']."</td>
                        <td>
                            <a href='index.php?uc=cv&action=modifier&table=formation&ligne=".$unEtudiant['id_formation']."' class='btn btn-xs btn-primary' name='modifier'><span class='glyphicon glyphicon-pencil'></span><img src='./styles/img/stylo.png'></i></a>&nbsp;
                            <a href='index.php?uc=cv&action=supprimer&table=formation&ligne=".$unEtudiant['id_formation']."' class='btn btn-xs btn-primary' name='supprimer'><span class='glyphicon glyphicon-trash'></span><img src='./styles/img/moins.png'></i></a>
                        </td>";
                echo "</tr>";
            }
        } else {
            echo "<button class='btn'>Rechercher</button><br><br>
            <table class='table table-striped table-bordered'>
                <thead>
                    <th>Diplôme</th>
                    <th>Année de début</th>
                    <th>Année de fin</th>
                    <th>Etablissement</th>
                </thead>
                <tbody>";

            $pdo = PdoSio::getPdoSio();
            if(is_numeric($recherche)) {
                $etudiants = $pdo->requeteSelection("SELECT * FROM cv_formation WHERE id_utilisateur =".$ligne." AND (AnneeDeb = ".$recherche." OR AnneeFin = ".$recherche.")");
            } else {
                $etudiants = $pdo->requeteSelection("SELECT * FROM cv_formation WHERE id_utilisateur =".$ligne." AND (Diplome LIKE '%".$recherche."%' OR Etab LIKE '%".$recherche."%')");
            }
            foreach ($etudiants as $unEtudiant) {
                echo "<tr>
                        <td>".$unEtudiant['Diplome']."</td>
                        <td>".$unEtudiant['AnneeDeb']."</td>
                        <td>".$unEtudiant['AnneeFin']."</td>
                        <td>".$unEtudiant['Etab']."</td>
                    </tr>";
            }
        }
    }
    echo "</div>";
    ?>
        <script type="text/javascript">
            $(document).ready(function() {
                $("button").click(function(){
                   var recherche = $("#recherche").val();
                   var request = $.ajax({
                        type: "POST",
                        url: './include/recherche_formationCV.php',
                        data: { id: "<?php echo $identifiant; ?>", ligne: "<?php echo $ligne; ?>", table: "formation", recherche: recherche}
                   });
                   request.done(function(resultat) {
                        $("#val").html(resultat);
                   });
                   request.fail(function(resultat) {
                       
                   });
                });
            });
        </script>
